using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class UI组件_渐变色选择 : MonoBehaviour
{
    public Image 背景;
    public Slider 进度条;

    public Color 初始色;
    public Color 结束色;

    public Image 输出色;

    private UnityAction<Color> 当颜色改变时;

    public void 初始化(Color 初始色,Color 结束色,UnityAction<Color> 当颜色改变时 = null)
    {
        this.初始色 = 初始色;
        this.结束色 = 结束色;
        this.当颜色改变时 = 当颜色改变时;
        float f = Random.value;
        输出色.color = Color.Lerp(初始色, 结束色, f);
        进度条.value = f;
        渲染背景色();
    }

    private void 渲染背景色()
    {
        if (背景.sprite != null) return;

        Texture2D texture2D = new Texture2D(50,1);
        for (int x = 0; x < 50; x++)
            texture2D.SetPixel(x, 0,Color.Lerp(初始色,结束色,x/49f));
        texture2D.Apply();

        背景.sprite = Texture2D_工具.贴图转精灵(texture2D);
    }

    public void 当值改变时()
    {
        输出色.color = Color.Lerp(初始色, 结束色, 进度条.value);
        当颜色改变时?.Invoke(输出色.color);
    }

    void Start()
    {

    }
}